﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using NetROS.Tiers.Data;
using NetROS.Tiers.Data.Bases;
using NetROS.Tiers.Entities;
using NetRosApp.Infrastructure.Interface;

namespace NetRosApp.FinPart.Services
{
   public class LocalUtility
    {
        public static TList<KnjizenjeStavka> Fill(IDataReader reader, TList<KnjizenjeStavka> rows, int start, int pageLength)
        {
            // advance to the starting row
            for (int i = 0; i < start; i++)
            {
                if (!reader.Read())
                    return rows; // not enough rows, just return
            }
            for (int i = 0; i < pageLength; i++)
            {
                if (!reader.Read())
                    break; // we are done
                string key = null;

                KnjizenjeStavka c = null;
                if (DataRepository.Provider.UseEntityFactory)
                {
                    key = new System.Text.StringBuilder("KnjizenjeStavka")
                    .Append("|").Append((reader.IsDBNull(((int)NetROS.Tiers.Entities.KnjizenjeStavkaColumn.ID - 1)) ? (int)0 : (System.Int32)reader[((int)NetROS.Tiers.Entities.KnjizenjeStavkaColumn.ID - 1)]).ToString()).ToString();
                    c = EntityManager.LocateOrCreate<KnjizenjeStavka>(
                    key.ToString(), // EntityTrackingKey
                    "KnjizenjeStavka",  //Creational Type
                    DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
                    DataRepository.Provider.EnableEntityTracking); // Track this entity?
                }
                else
                {
                    c = new NetROS.Tiers.Entities.KnjizenjeStavka();
                }

                if (!DataRepository.Provider.EnableEntityTracking ||
                c.EntityState == EntityState.Added ||
                (DataRepository.Provider.EnableEntityTracking &&
                ((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
                (DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
                                    c.EntityState == EntityState.Changed)))))
                {
                    c.SuppressEntityEvents = true;
                    c.ID = (System.Int32)reader["ID"];
                    c.IDKonto = (System.Int32)reader["IDKonto"];
                    c.IDOrganizacija = (System.Int32)reader["IDOrganizacija"];
                    c.IDVrstaFinDokumenta = (System.Int32)reader["IDVrstaFinDokumenta"];
                    c.DatumDokumentaOriginal = (System.DateTime)reader["DatumDokumentaOriginal"];
                    c.DatumDokumentaPromjenjen = reader.IsDBNull(reader.GetOrdinal("DatumDokumentaPromjenjen")) ? null : (System.DateTime?)reader["DatumDokumentaPromjenjen"];
                    c.Nalog = (System.String)reader["Nalog"];
                    c.OpisKnjizenja = reader.IsDBNull(reader.GetOrdinal("OpisKnjizenja")) ? null : (System.String)reader["OpisKnjizenja"];
                    c.Dug = (System.Decimal)reader["Dug"];
                    c.Pot = (System.Decimal)reader["Pot"];
                    c.IDKlijent = reader.IsDBNull(reader.GetOrdinal("IDKlijent")) ? null : (System.Int32?)reader["IDKlijent"];
                    c.SifraOriginalnogDokumenta = reader.IsDBNull(reader.GetOrdinal("SifraOriginalnogDokumenta")) ? null : (System.String)reader["SifraOriginalnogDokumenta"];
                    c.DatumDospijeca = reader.IsDBNull(reader.GetOrdinal("DatumDospijeca")) ? null : (System.DateTime?)reader["DatumDospijeca"];
                    c.PlacanjeModel = reader.IsDBNull(reader.GetOrdinal("PlacanjeModel")) ? null : (System.String)reader["PlacanjeModel"];
                    c.PlacanjePoziv = reader.IsDBNull(reader.GetOrdinal("PlacanjePoziv")) ? null : (System.String)reader["PlacanjePoziv"];
                    c.IDKnjizenja = (System.Int32)reader["IDKnjizenja"];
                    c.DatumCreated = (System.DateTime)reader["DatumCreated"];
                    c.DatumModified = (System.DateTime)reader["DatumModified"];
                    c.IDUserCreated = (System.Int32)reader["IDUserCreated"];
                    c.IDUserModified = (System.Int32)reader["IDUserModified"];
                    c.PrikazUKnjizi = (System.Int32)reader["PrikazUKnjizi"];
                    c.DateUICreated = (System.DateTime)reader["DateUICreated"];
                    c.KontoSifraPrijenos = reader.IsDBNull(reader.GetOrdinal("KontoSifraPrijenos")) ? null : (System.String)reader["KontoSifraPrijenos"];
                    c.IDDokument = reader.IsDBNull(reader.GetOrdinal("IDDokument")) ? null : (System.Int32?)reader["IDDokument"];
                    c.Calculated = (System.String)reader["Calculated"];
                    c.EntityTrackingKey = key;

                    VwPreostaloZaZatvaranjeZapisanoSimple ventity = null;
                    if (DataRepository.Provider.UseEntityFactory)
                    {
                        ventity = EntityManager.CreateViewEntity<VwPreostaloZaZatvaranjeZapisanoSimple>("VwPreostaloZaZatvaranjeZapisanoSimple", DataRepository.Provider.EntityCreationalFactoryType);
                    }
                    else
                    {
                        ventity = new VwPreostaloZaZatvaranjeZapisanoSimple();
                    }
                    ventity.IDKnjizStavkaSimpl = reader.IsDBNull(reader.GetOrdinal("Preostalo")) ? 0 : (System.Int32)reader["IDKnjizStavkaSimpl"];
                    //ventity.IDKnjizStavkaSimpl = (System.Int32)reader["IDKnjizStavkaSimpl"];
                    //entity.IDKnjizStavkaSimpl = (Convert.IsDBNull(reader["IDKnjizStavkaSimpl"]))?(int)0:(System.Int32)reader["IDKnjizStavkaSimpl"];
                    ventity.Preostalo = reader.IsDBNull(reader.GetOrdinal("Preostalo")) ? null : (System.Decimal?)reader["Preostalo"];
                    //entity.Preostalo = (Convert.IsDBNull(reader["Preostalo"]))?0:(System.Decimal?)reader["Preostalo"];
                    ventity.AcceptChanges();
                   // Dictionary<string, object> _tmpd = new Dictionary<string, object>();
                   // _tmpd.Add('1',);
                    c.Tag = new EventArgs<string, string, VwPreostaloZaZatvaranjeZapisanoSimple>(reader.IsDBNull(reader.GetOrdinal("PrikaznoIme")) ? null : (System.String)reader["PrikaznoIme"], reader.IsDBNull(reader.GetOrdinal("ImeKlijentaMB")) ? null : (System.String)reader["ImeKlijentaMB"], ventity);

                    c.AcceptChanges();
                    c.SuppressEntityEvents = false;
                }
                rows.Add(c);
            }
            return rows;
        }
    }
}
